Skip to content

feat(manage_editor): add save_prefab_stage action#990

Merged
Scriptwonder merged 3 commits intoCoplayDev:betafrom
zaferdace:feat/save-prefab-stage
Mar 28, 2026
Merged

feat(manage_editor): add save_prefab_stage action#990
Scriptwonder merged 3 commits intoCoplayDev:betafrom
zaferdace:feat/save-prefab-stage

Conversation

@zaferdace
Copy link
Copy Markdown
Contributor

@zaferdace zaferdace commented Mar 26, 2026

Summary

Complements #968 (open_prefab_stage) by adding the missing save_prefab_stage action to manage_editor, completing the prefab stage edit workflow.

What save_prefab_stage does

  • Checks if a prefab stage is currently open via PrefabStageUtility.GetCurrentPrefabStage()
  • Returns ErrorResponse immediately if no stage is open (guides the caller to use open_prefab_stage first)
  • Marks the prefab stage scene dirty with EditorSceneManager.MarkSceneDirty
  • Calls EditorSceneManager.SaveScene and checks the return value
  • Returns ErrorResponse if the save failed (read-only file, full disk, etc.)
  • Returns SuccessResponse with prefabPath and saved on success

Full prefab stage workflow

manage_editor(action="open_prefab_stage", path="Assets/Prefabs/MyPrefab.prefab")
  → manage_gameobject / manage_components  (modify objects inside)
  → manage_editor(action="save_prefab_stage")
  → manage_editor(action="close_prefab_stage")

Changes

  • C#: SavePrefabStage() method + case "save_prefab_stage" in ManageEditor.cs
  • Python: save_prefab_stage added to action Literal and tool description in manage_editor.py
  • Tests: 3 new Python tests — description advertises the action, forwarding to Unity works, no spurious params included

Test plan

  • Python tests: 28 passed (includes 3 new save_prefab_stage tests)
  • Open a prefab via open_prefab_stage, modify an object, call save_prefab_stage, verify changes persist in the .prefab asset
  • Call save_prefab_stage with no stage open — verify ErrorResponse with helpful message
  • Verify close_prefab_stage still works unchanged after saving

Notes

open_prefab_stage was merged in #968. This PR only adds save_prefab_stage and is based on beta.

Summary by Sourcery

Add support for saving the currently open Unity prefab stage through the manage_editor tool and wire it through to the Unity editor backend.

New Features:

  • Expose a new save_prefab_stage action in the manage_editor tool API to save changes in the active prefab stage back to the prefab asset.

Enhancements:

  • Update the Unity editor command handler and error messaging to recognize save_prefab_stage alongside other prefab stage actions.

Tests:

  • Add Python tests to ensure save_prefab_stage is documented in the manage_editor tool description, correctly forwards to Unity, and omits unrelated parameters in its payload.

Summary by CodeRabbit

  • New Features

    • Added a "save prefab stage" action to save changes from the open prefab editing stage back to the prefab asset.
  • Documentation

    • Updated tool, API and reference docs to include the new save action in the prefab workflow.
  • Tests

    • Added tests confirming the new action is advertised, forwarded correctly, and omits unrelated parameters.
  • Chores

    • CLI behavior: removed the previous force flag for prefab save and updated the success message to "Saved prefab stage".

Adds save_prefab_stage to manage_editor to complete the prefab stage
workflow alongside the existing open_prefab_stage and close_prefab_stage.

- C#: SavePrefabStage() uses EditorSceneManager.MarkSceneDirty +
  SaveScene on the prefab stage scene, returns ErrorResponse when no
  stage is open or save fails
- Python: adds save_prefab_stage to action Literal and tool description
- Tests: 3 new tests covering forwarding, description, and clean params

open_prefab_stage was already merged in CoplayDev#968. This PR only adds
save_prefab_stage.
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 26, 2026

Reviewer's Guide

Adds a new manage_editor action save_prefab_stage that saves the currently open Unity prefab stage, wires it through the Python tool surface, and adds tests to verify description, forwarding behavior, and parameter filtering.

Sequence diagram for save_prefab_stage tool call workflow

sequenceDiagram
    actor User
    participant Server_manage_editor_tool
    participant UnityConnection
    participant Unity_ManageEditor

    User->>Server_manage_editor_tool: manage_editor(action=save_prefab_stage)
    Server_manage_editor_tool->>UnityConnection: async_send_command_with_retry(action=save_prefab_stage)
    UnityConnection->>Unity_ManageEditor: HandleCommand(params.action=save_prefab_stage)
    Unity_ManageEditor->>Unity_ManageEditor: SavePrefabStage()
    Unity_ManageEditor-->>UnityConnection: SuccessResponse | ErrorResponse
    UnityConnection-->>Server_manage_editor_tool: response JSON
    Server_manage_editor_tool-->>User: tool result (prefabPath, saved | error message)
Loading

Class diagram for ManageEditor SavePrefabStage integration

classDiagram
    class ManageEditor {
        +static object HandleCommand(JObject params)
        -static object OpenPrefabStage(string requestedPath)
        -static object SavePrefabStage()
        -static object ClosePrefabStage()
    }

    class ErrorResponse {
        +string message
        +ErrorResponse(string message)
    }

    class SuccessResponse {
        +string message
        +object data
        +SuccessResponse(string message, object data)
    }

    ManageEditor ..> ErrorResponse : returns
    ManageEditor ..> SuccessResponse : returns
Loading

Flow diagram for SavePrefabStage Unity editor logic

flowchart TD
    Start([Start SavePrefabStage])
    GetStage[Get current prefab stage via PrefabStageUtility.GetCurrentPrefabStage]
    NoStage[Return ErrorResponse: not in prefab editing mode]
    GetPath[Read prefabPath from prefabStage.assetPath]
    MarkDirty["MarkSceneDirty(prefabStage.scene)"]
    SaveScene["Call EditorSceneManager.SaveScene(prefabStage.scene)"]
    SaveFailed[Return ErrorResponse: save failed]
    SaveSuccess[Return SuccessResponse with prefabPath and saved=true]
    ExceptionNode[Return ErrorResponse with exception message]

    Start --> GetStage
    GetStage -->|prefabStage is null| NoStage
    GetStage -->|prefabStage exists| GetPath
    GetPath --> MarkDirty --> SaveScene
    SaveScene -->|saved == false| SaveFailed
    SaveScene -->|saved == true| SaveSuccess

    Start --> ExceptionCheck{Exception thrown?}
    ExceptionCheck -->|yes| ExceptionNode
    ExceptionCheck -->|no| GetStage
Loading

File-Level Changes

Change Details Files
Expose new save_prefab_stage action from the Python manage_editor tool to Unity and document it in the tool description and action Literal.
  • Extend manage_editor tool description string to mention save_prefab_stage and describe its behavior in the prefab workflow.
  • Add save_prefab_stage to the Literal action union so callers can invoke it from Python/clients.
  • Keep the existing optional parameters (tool_name, tag_name, layer_name, path) unchanged so the new action reuses the same call surface without sending them if unused.
Server/src/services/tools/manage_editor.py
Implement SavePrefabStage behavior in the Unity-side ManageEditor handler and register the new action in the dispatcher and error message.
  • Add a new switch case for save_prefab_stage in HandleCommand that calls SavePrefabStage().
  • Extend the unknown-action ErrorResponse message to list save_prefab_stage as a supported action.
  • Implement SavePrefabStage to validate an open prefab stage, mark the scene dirty, call EditorSceneManager.SaveScene, and return either ErrorResponse on failure or SuccessResponse with prefabPath and saved on success, with a generic catch for unexpected exceptions.
MCPForUnity/Editor/Tools/ManageEditor.cs
Add tests to ensure save_prefab_stage is advertised, correctly forwarded to Unity, and does not send spurious parameters.
  • Include save_prefab_stage in UNITY_FORWARDED_ACTIONS so it is treated as a Unity-forwarded manage_editor action.
  • Add a test that manage_editor’s tool description string contains save_prefab_stage.
  • Add a test that calling manage_editor(action="save_prefab_stage") forwards the action to Unity successfully.
  • Add a test asserting that the forwarded params omit irrelevant keys like toolName, tagName, layerName, prefabPath, and path for save_prefab_stage.
Server/tests/test_manage_editor.py

Possibly linked issues

  • #(unlisted): PR’s save_prefab_stage, together with open_prefab_stage, enables editing prefab contents and persisting serialized field changes.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 26, 2026

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 61e1b0f7-5d30-43d6-a26f-58c61f016378

📥 Commits

Reviewing files that changed from the base of the PR and between 242548b and 0dd22dc.

📒 Files selected for processing (1)
  • Server/src/cli/commands/prefab.py

📝 Walkthrough

Walkthrough

Adds a new save_prefab_stage action to ManageEditor: Unity editor plugin saves the active PrefabStage scene via EditorSceneManager.SaveScene; server tool definitions, tests, CLI, and docs updated to include and forward the new action. Error handling added for missing stage or save failures.

Changes

Cohort / File(s) Summary
Unity Editor Plugin
MCPForUnity/Editor/Tools/ManageEditor.cs
Added save_prefab_stage handling: fetches current PrefabStage, errors if none, marks the stage scene dirty, calls EditorSceneManager.SaveScene, returns { prefabPath, saved } on success or an error/exception message on failure; updated unknown-action text.
Server Tool Definition
Server/src/services/tools/manage_editor.py
Added "save_prefab_stage" to the manage_editor tool action Literal and updated the tool description/documentation.
Server CLI
Server/src/cli/commands/prefab.py
Switched save routing to manage_editor with action="save_prefab_stage", removed --force/-f option and force parameter, updated success message to "Saved prefab stage".
Tests
Server/tests/test_manage_editor.py
Added tests asserting save_prefab_stage is advertised and forwarded to Unity with params["action"] == "save_prefab_stage" and that unrelated optional params are omitted.
Docs / Resources
Server/src/services/resources/prefab.py, unity-mcp-skill/references/tools-reference.md
Updated prefab API docs and tools reference to include save_prefab_stage in the open/save/close prefab-stage workflow description.

Sequence Diagram

sequenceDiagram
    participant Client as Client/LLM
    participant Server as MCP Server
    participant Tool as manage_editor Tool
    participant Unity as Unity Editor
    participant Editor as EditorSceneManager

    Client->>Server: request save_prefab_stage
    Server->>Tool: manage_editor(action="save_prefab_stage")
    Tool->>Unity: forward command { action: "save_prefab_stage" }
    Unity->>Unity: Get current PrefabStage
    alt PrefabStage exists
        Unity->>Unity: Mark stage.scene dirty
        Unity->>Editor: SaveScene(stage.scene)
        Editor-->>Unity: SaveScene result
        Unity-->>Tool: { prefabPath, saved: true }
    else No PrefabStage active
        Unity-->>Tool: error (no active prefab stage)
    end
    Tool-->>Server: response
    Server-->>Client: final result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • dsarno
  • justinpbarnett

Poem

🐇 I hopped into the prefab stage at night,
I nudged the scene and gave it light,
A save command clicked — a gentle cheer,
The prefab sleeps safe, no fear,
Hooray! Disk and rabbit both delight.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.73% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a new save_prefab_stage action to the manage_editor tool.
Description check ✅ Passed The pull request description comprehensively covers the change type (new feature), specific changes made, test plan with results, documentation updates, and clear context linking to related work.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

The CLI `prefab save` command was routing to manage_prefabs with
action=save_open_stage, which has no C# handler. Rewire it to
manage_editor action=save_prefab_stage which was added in this PR.

Removed the --force flag since the C# implementation always marks
dirty before saving (MarkSceneDirty + SaveScene).
@Scriptwonder Scriptwonder merged commit e2903a7 into CoplayDev:beta Mar 28, 2026
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants